package com.wechat.service;

import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.wechat.dao.UserBalanceRecordDao;
import com.wechat.entity.Question;
import com.wechat.entity.User;
import com.wechat.entity.UserBalanceRecord;
import com.wechat.entity.UserRechargeRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 *
 * @author cgc
 * @date 18/4/9
 */
@Service
public class UserBalanceService {

    @Autowired
    private UserBalanceRecordDao userBalanceRecordDao;

    @Autowired
    private PayService payService;
    @Autowired
    private UserService userService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserRechargeService userRechargeService;

    /**
     *
     */
    public int deleteById(Integer id) {
        return userBalanceRecordDao.deleteById(id);
    }

    /**
     *
     */
    public int save(UserBalanceRecord record) {
        return userBalanceRecordDao.save(record);
    }

    /**
     *
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public UserBalanceRecord findById(Integer id) {
        return userBalanceRecordDao.findById(id);
    }

    /**
     *
     */
    public int update(UserBalanceRecord record) {
        return userBalanceRecordDao.update(record);
    }

    /**
     * 问题超时退款
     * @param question
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean questionRefund(Question question) throws WxPayException{

        User user = userService.findById(question.getUserId());
        //增加账户财务流水
        UserBalanceRecord balanceRecord = new UserBalanceRecord();
        balanceRecord.setBalance(user.getBalance());
        balanceRecord.setUserId(user.getId());
        balanceRecord.setAmount(question.getAccountAmount());
        balanceRecord.setTitle("红包金额退回用户余额");
        balanceRecord.setRecordType(UserBalanceRecord.RECORD_TYPE_REDPACKETIN_REFUND);
        balanceRecord.setQuestionId(question.getId());
        balanceRecord.setRechargeId(question.getRechargeId());
        this.save(balanceRecord);
        UserBalanceRecord balanceRecord1 = new UserBalanceRecord();
        balanceRecord1.setBalance(user.getBalance());
        balanceRecord1.setUserId(user.getId());
        balanceRecord1.setAmount(question.getAccountAmount().negate());
        balanceRecord1.setTitle("余额退款");
        balanceRecord1.setRecordType(UserBalanceRecord.RECORD_TYPE_REFUND);
        balanceRecord1.setQuestionId(question.getId());
        balanceRecord1.setRechargeId(question.getRechargeId());
        this.save(balanceRecord1);

        String body = "问题红包超时退款";

        UserRechargeRecord rechargeRecord = userRechargeService.findById(question.getRechargeId());
        Integer totalFee = rechargeRecord.getRealFee().multiply(new BigDecimal(100)).intValue();
        Integer refundFee = question.getAccountAmount().multiply(new BigDecimal(100)).intValue();
        //将用户退款的流水号作为退款单号
        WxPayRefundResult result  =payService.miniAppRefund(body,question.getRechargeId().toString(),balanceRecord1.getId().toString(),totalFee,refundFee);
        if(result.getOutTradeNo().equals(question.getRechargeId().toString())){
            UserBalanceRecord record = new UserBalanceRecord();
            record.setId(balanceRecord1.getId());
            //流水记录备注字段放微信退款单号
            record.setRemark(result.getTransactionId());
            this.update(record);
            Question q = new Question();
            q.setId(question.getId());
            q.setAccountAmount(new BigDecimal(0));
            q.setAmountStatus(Question.AMOUNT_STATUS_RETURN);

            questionService.updateQuestion(q);

            return true;
        }
        return false;
    }
}
